package com.sjz.test2;

import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.Test;

import com.sjz.test2.Employee.Status;

public class TestStreamAPI3 {
	
	List<Employee> emps = Arrays.asList(
			new Employee(102, "李四", 79, 6666.66, Status.BUSY),
			new Employee(101, "张三", 18, 9999.99, Status.FREE),
			new Employee(103, "王五", 28, 3333.33, Status.VOCATION),
			new Employee(104, "赵六", 8, 7777.77, Status.BUSY),
			new Employee(104, "赵六", 8, 7777.77, Status.FREE),
			new Employee(104, "赵六", 8, 7777.77, Status.FREE),
			new Employee(105, "田七", 38, 5555.55, Status.BUSY)
	);
	
	//3. 终止操作
		/*
			归约
			reduce(T identity, BinaryOperator) / reduce(BinaryOperator) ——可以将流中元素反复结合起来，得到一个值。
		 */
	@Test
	public void test1() {
		List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
		//  R apply(T t, U u);
		Integer reduce = list.stream().reduce(0,(x,y)->x+y);
		System.out.println(reduce);   //求和
		System.out.println("----------------------------------------");
		Optional<Double> reduce2 = emps.stream().map(Employee::getSalary)
		.reduce(Double::sum);//得到薪水的综合
		System.out.println(reduce2);
		
	}
	
	//需求：搜索名字中 “六” 出现的次数
		@Test
		public void test2(){
		 	long count = emps.stream().filter((x)->x.getName().contains("六")).count();
	     	System.out.println("搜索名字中 “六” 出现的次数:"+count);
			System.out.println("-----------------------------------------------");
			Optional<Integer> reduce = emps.stream()
			.map(Employee::getName)
			.flatMap(TestStreamAPI1::filterCharacter)   //flatMap不能改成map
			.map((ch)->{
				if(ch.equals('六'))
					return 1;
				else
					return 0;
			})
			.reduce(Integer::sum);
			System.out.println("搜索名字中 “六” 出现的次数:"+reduce.get());
		}
		
		//collect——将流转换为其他形式。接收一个 Collector接口的实现，用于给Stream中元素做汇总的方法
		@Test
		public void test3() {
			List<String> list = emps.stream().map(Employee::getName)
			.collect(Collectors.toList());   //转换成list
			System.out.println(list);  
			//list.forEach(System.out::println);
			System.out.println("----------------------------------");
			Set<String> set = emps.stream()
				.map(Employee::getName)
				.collect(Collectors.toSet());//转换成Set
			set.forEach(System.out::println);
			System.out.println("----------------------------------");

			HashSet<String> hs = emps.stream()
				.map(Employee::getName)
				.collect(Collectors.toCollection(HashSet::new));//转换成HashSet
			hs.forEach(System.out::println);
		}
		
		
		@Test
		public void test4() {
			Optional<Double> collect = emps.stream().map(Employee::getSalary)
			.collect(Collectors.maxBy(Double::compare));
			System.out.println("最多薪水的是："+collect.get());
			System.out.println("**********************************************");
			Optional<Employee> collect2 = emps.stream().collect(Collectors.minBy((e1,e2)->Double.compare(e1.getSalary(), e2.getSalary())));
		     System.out.println("薪水最少的是"+collect2.get());
		     
		     Double collect3 = emps.stream().collect(Collectors.summingDouble(Employee::getSalary));
		     System.out.println("薪水总和是："+collect3);
		     Double collect4 = emps.stream().collect(Collectors.averagingDouble(Employee::getSalary));
		     System.out.println("平均薪水是"+collect4);
		     Long collect5 = emps.stream().collect(Collectors.counting());
		     System.out.println("总个数"+collect5);
		     DoubleSummaryStatistics dss = emps.stream().collect(Collectors.summarizingDouble(Employee::getSalary));
		 	 System.out.println("最大薪水是"+dss.getMax());
		}
		
		
		/***
		 * 分组
		 */
		@Test
		public void test5() {
			// R apply(T t);
			Map<Status, List<Employee>> map  =emps.stream().collect(Collectors.groupingBy(Employee::getStatus));
			System.out.println(map);
		}
		
		//多级分组 
		@Test
		public void test6() {
			Map<Status, Map<String, List<Employee>>> map = emps.stream().collect(Collectors.groupingBy(Employee::getStatus,Collectors.groupingBy((e)->{
				if(e.getAge()>=60) {
					return "老年 ";
				}else if(e.getAge()>=35) {
					return "中年";
				}else {
					return "成年";
				}
			})));
			//{VOCATION={成年=[Employee [id=103, name=王五, age=28, salary=3333.33, status=VOCATION]]}, 
			//FREE={成年=[Employee [id=101, name=张三, age=18, salary=9999.99, status=FREE], Employee [id=104, name=赵六, age=8, salary=7777.77, status=FREE], Employee [id=104, name=赵六, age=8, salary=7777.77, status=FREE]]}, 
			//BUSY={老年 =[Employee [id=102, name=李四, age=79, salary=6666.66, status=BUSY]], 成年=[Employee [id=104, name=赵六, age=8, salary=7777.77, status=BUSY]], 中年=[Employee [id=105, name=田七, age=38, salary=5555.55, status=BUSY]]}}
			System.out.println(map);
		}
		
		
		
		 //分区
         //结果 {false=[Employee [id=103, name=王五, age=28, salary=3333.33, status=VOCATION]], 
		//true=[Employee [id=102, name=李四, age=79, salary=6666.66, status=BUSY], Employee [id=101, name=张三, age=18, salary=9999.99, status=FREE], Employee [id=104, name=赵六, age=8, salary=7777.77, status=BUSY], Employee [id=104, name=赵六, age=8, salary=7777.77, status=FREE], Employee [id=104, name=赵六, age=8, salary=7777.77, status=FREE], Employee [id=105, name=田七, age=38, salary=5555.55, status=BUSY]]}
		@Test
		public void test7() {
               Map<Boolean, List<Employee>> map = emps.stream().collect(Collectors.partitioningBy((e)->e.getSalary() >= 5000));
               System.out.println(map);
		}
		
		
		/***
		 * joinning() 无参数方法会返回一个 Collectors 实例，并且以空字符串 ( "" ) 来拼接收集到的所有元素
		 */
		@Test
		public void test8() {
			 List<String> list = Arrays.asList("1","2","3","4");
		        String result= list.stream().collect(Collectors.joining());
		        System.out.println(result); //结果  1234
		        System.out.println("-----------------------");
		        String str = emps.stream()
		    			.map(Employee::getName)
		    			.collect(Collectors.joining("," , "----", "****"));  //中间已逗号拼接  前缀是第二个参数  后缀是第三参数  结果  ----李四,张三,王五,赵六,赵六,赵六,田七****
		    		System.out.println(str);
		    		
		    		 String str1 = emps.stream()
				    			.map(Employee::getName)
				    			.collect(Collectors.joining(","));   //中间已逗号拼接   //结果  李四,张三,王五,赵六,赵六,赵六,田七
				    		System.out.println(str1);
		}
		
		
		/****
		 * 薪水总和
		 */
		@Test
		public void test9() {
			Optional<Double> collect = emps.stream().map(Employee::getSalary)
			.collect(Collectors.reducing(Double::sum));
			System.out.println(collect.get());
		}
		
}
